
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';

import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';

////////////////////////////////////////////////////////////////////////////////
// import atoms
// 初始化
////////////////////////////////////////////////////////////////////////////////
import set from '@dfeidao/atom-web/global/set';
import s_put from '@dfeidao/atom-web/storage/put';
import P01 from './P01';
import P04 from './P04';
import P05 from './P05';

export default async function a000(fd: IFeidaoAiBrowserComponent) {
	const productid = fd.data.node.querySelector<HTMLSpanElement>('[data-feidao-id="user"]').getAttribute('prid');
	// const user = await whoami(get_user_info);
	// console.log(user);
	await s_put('productid', productid);
	set('productid', productid);
	await s_put('productid', productid);

	const page_no = 1;
	const page_size = 20;

	const query = {
		productid,
		page_no,
		page_size
	};
	const res = await nodejs<{ dataArray: object[], page_no: number, page_size: number }>('functional-permissions/zj-000001/s001', query);

	res.page_size = page_size;
	res.page_no = page_no;


	if (res.dataArray.length === 0) {
		render(fd, '', P01, 'P01', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="P05"]').innerHTML = '';
		render(fd, '', P04, 'P04', 'inner');
	} else {
		const tb = res.dataArray;
		const data = tb.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		render(fd, data, P01, 'P01', 'inner');
		if (res.dataArray.length >= 20) {
			render(fd, res, P05, 'P05', 'inner');
		}
		fd.data.node.querySelector('[data-feidao-presentation="P04"]').innerHTML = '';
	}
}
